import router from '@system.router';

let timer = null
let spendTimer = null
let percentTimer = null

export default {
    data: {
        second: 0,
        secondVisible: true,
        spendTimeList: [6, 4, 2], // 呼吸速度
        breathingTypeList: ['呼气', '吸气', '完成'],
        breathingType: 0,
        percent: 0, // 百分比
        duration: 6000, // logo 转一圈的时长
        count: 5 // 转动次数
    },
    onInit() {
        console.info("training页面 onInit")
        this.second = this.time * 60
        this.duration = this.spendTimeList[this.spend] * 1000
        this.count = this.time * 60 / this.spendTimeList[this.spend]
    },
    onReady() {
        console.info("training页面 onReady")
        this.initTimer()
    },
    onShow() {
        console.info("training页面 onShow")
    },
    onHide() {
        console.info("training页面 onHide")
    },
    onDestroy() {
        console.info("training页面 onDestroy")
        clearInterval(timer)
        clearInterval(spendTimer)
        clearInterval(percentTimer)
    },
    initTimer() {
        timer = setInterval(()=>{
            this.second -= 1
            if (this.second <= 0) {
                this.secondVisible = false
                this.percent = 100
                clearInterval(timer)
                clearInterval(spendTimer)
                clearInterval(percentTimer)
                this.breathingType = 2
            }
        },1000)

        spendTimer = setInterval(()=>{
            this.breathingType = this.breathingType ? 0 : 1
            this.percent = 0
        },1000 * this.spendTimeList[this.spend])

        percentTimer = setInterval(()=>{
            this.percent++
        },1000 * this.spendTimeList[this.spend] / 100)
    },
    handleClick() {
        clearInterval(timer)
        clearInterval(spendTimer)
        clearInterval(percentTimer)
        router.replace({
            uri: "pages/index/index"
        })
    },
    handleSwiper(e) {
        console.log(JSON.stringify(e))
        console.log(this.second)
        if (this.second <= 0) {
            if (e.direction === 'right') {
            } else if (e.direction === 'left') {
                this.handleClick()
            }
        }
    }
}
